import cv2

import numpy as np


# BGR转化为灰度图像
def BGR2GRAY(img):

    b = img[:, :, 0].copy()

    g = img[:, :, 1].copy()

    r = img[:, :, 2].copy()

    # Gray scale

    # out = 0.2126 * r + 0.7152 * g + 0.0722 * b

    # 识别 111.webp
    # out = 0.299 * r + 0.587 * g + 0.114 * b
    out = 0.299 * r + 0.587 * g + 0.114 * b


    out = out.astype(np.uint8)

    return out

# 二值化灰度图像
# def binarization(img, th=128):

# 识别 111.webp
# def binarization(img, th=10):

def binarization(img, th=70):

    img[img < th] = 0

    img[img >= th] = 255

    return img

# Read image

img = cv2.imread("000484577664.jpg").astype(np.float64)

# Grayscale

out = BGR2GRAY(img)

# Binarization

out = binarization(out)

# Save result

cv2.imwrite("out.jpg", out)

cv2.imshow("result", out)

cv2.waitKey(0)

cv2.destroyAllWindows()

